package com.oasystem.web.controller.profit;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.oasystem.domain.PagingBean;
import com.oasystem.domain.ProfitDetail;
import com.oasystem.domain.ProfitRecord;
import com.oasystem.service.ProfitService;
import com.oasystem.utils.StringUtils;
import com.oasystem.web.view.ProfitTableExcelView;
import com.oasystem.web.view.ProfitTableGbptView;

@Controller
@RequestMapping(value = "/MainMenu/profit")
public class ProfitAddController {
	@Resource(name = "profitService")
	private ProfitService profitService;

	@RequestMapping(value = "/generateProfitTable")
	public String generateProfitTable(String countDate,
			double yesterSelledAmount, double profitReturnPercent,
			String pageIndex, Model model) {
		int shouldReturnProfitMemberCount = profitService
				.retrieveShouldReturnProfitMemberCount(countDate);
		ProfitRecord pr = new ProfitRecord();
		pr.setAcceptReturnMemberCount(shouldReturnProfitMemberCount);
		pr.setReturnPercent(profitReturnPercent);
		pr.setYesterdaySelledAmount(yesterSelledAmount);

		BigDecimal totalReturnAmount = new BigDecimal(0);
		BigDecimal selledAmount = new BigDecimal(yesterSelledAmount);
		BigDecimal profitPercent = new BigDecimal(profitReturnPercent);
		BigDecimal hundred = new BigDecimal(100);
		profitPercent = profitPercent.divide(hundred);
		totalReturnAmount = selledAmount.multiply(profitPercent);
		totalReturnAmount.setScale(2, BigDecimal.ROUND_HALF_UP);

		pr.setTotalReturnAmount(totalReturnAmount.doubleValue());
		PagingBean<ProfitDetail> pagingBean = new PagingBean<ProfitDetail>();
		pagingBean.setCurrentPage(Integer.valueOf(pageIndex));
		profitService.generateProfitDetails(countDate, pr, pagingBean);
		model.addAttribute("profitID", pr.getId());
		model.addAttribute("pagingBean", pagingBean);
		model.addAttribute("yesterSelledAmount", yesterSelledAmount);
		model.addAttribute("profitReturnPercent", profitReturnPercent);
		model.addAttribute("pageIndex", pageIndex);
		return "profit.profitTable";
	}

	@RequestMapping(value = "/returnHistory")
	public String returnHistory(String memberID, Model model) {
		model.addAttribute("historyList",
				profitService.getReturnHistory(memberID));
		return "profit.returnHistory";
	}

	@RequestMapping(value = "/totalSelledAmount")
	public @ResponseBody
	ModelMap totalSelledAmount(String countDate, ModelMap model) {
		double sum = 0.00D;
		try {
			sum = profitService.getSelledAmountByDate(countDate);
			BigDecimal totalSelled = new BigDecimal(sum);
			totalSelled.setScale(2, BigDecimal.ROUND_HALF_UP);

			model.addAttribute("sum", totalSelled.doubleValue());
		} catch (Exception e) {
			model.addAttribute("error", e.getMessage());
		}
		return model;
	}

	@RequestMapping(value = "/shouldReturnProfitMemberCount")
	public @ResponseBody
	ModelMap shouldReturnProfitMemberCount(String countDate, ModelMap model) {
		try {
			int shouldReturnProfitMemberCount = profitService
					.retrieveShouldReturnProfitMemberCount(countDate);
			model.addAttribute("shouldReturnProfitMemberCount",
					shouldReturnProfitMemberCount);
		} catch (Exception e) {
			model.addAttribute("error", e.getMessage());
		}
		return model;
	}

	@RequestMapping(value = "/updateReturnProfitAmount")
	public @ResponseBody
	ModelMap updateReturnProfitAmount(String memberIDs, String newAmount,
			ModelMap model) {
		try {
			profitService.updateReturnProfitAmount(memberIDs,
					Double.valueOf(newAmount));
		} catch (Exception e) {
			model.addAttribute("error", e.getMessage());
		}
		return model;
	}

	@RequestMapping(value = "/batchReturnProfit")
	public @ResponseBody
	ModelMap batchReturnProfit(String countDate, String memberIDs,
			String profitID, ModelMap model) {
		try {
			profitService.batchReturnProfit(StringUtils.removeSpace(countDate),
					memberIDs, profitID);
		} catch (Exception e) {
			model.addAttribute("error", e.getMessage());
		}
		return model;
	}

	@RequestMapping(value = "/returnAllMemberProfit")
	public @ResponseBody
	ModelMap returnAllMemberProfit(String countDate, String profitID,
			ModelMap model) {
		try {
			// TODO: get member ids that should return current day
			// then call following method
			profitService.returnAllMemberProfit(
					StringUtils.removeSpace(countDate), profitID);
		} catch (Exception e) {
			model.addAttribute("error", e.getMessage());
		}
		return model;
	}

	@RequestMapping(value = "/pagingList")
	public String pagingList(String countDate, double yesterSelledAmount,
			double profitReturnPercent, String profitID, String pageIndex,
			Model model) {
		PagingBean<ProfitDetail> pagingBean = new PagingBean<ProfitDetail>();
		pagingBean.setCurrentPage(Integer.valueOf(pageIndex));
		profitService
				.pagingList(pagingBean, StringUtils.removeSpace(countDate));
		model.addAttribute("pagingBean", pagingBean);
		model.addAttribute("yesterSelledAmount", yesterSelledAmount);
		model.addAttribute("profitReturnPercent", profitReturnPercent);
		model.addAttribute("pageIndex", pageIndex);
		model.addAttribute("profitID", profitID);
		return "profit.profitTable";
	}

	@RequestMapping(value = "/generateChart")
	public ModelAndView generateChart() {
		// ModelAndView mv = new ModelAndView();
		// mv.setView(new ProfitReturnedTodayExcelView());
		// mv.addObject("profitReturnRecordToday",
		// profitService.retrieveProfitReturnRecordToday());
		// return mv;
		ModelAndView mv = new ModelAndView();
		mv.setView(new ProfitTableGbptView());
		mv.addObject("profitDetails", profitService.retrieveProfitReturnRecordToday());
		return mv;
	}

	@RequestMapping(value = "/generateProfitReturnTable")
	public ModelAndView generateProfitReturnTable() {
		ModelAndView mv = new ModelAndView();
		mv.setView(new ProfitTableExcelView());
		mv.addObject("profitDetails", profitService.retrieveProfitDetails());
		return mv;
	}
}
